<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible=true">增加停车计费规则</el-button>
      <el-button @click="exportToExcelBtn">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ chargeTypeMethod(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editBtn(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delBtn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right; padding: 10px 0">
        <el-pagination
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pageSize"
          layout="total,prev, pager, next"
          :total="total"
          :current-page="params.page"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <add-rule ref="addRule" :dialog-visible.sync="dialogVisible" :current-id="currentId" @submit-btn="getRuleList" />
  </div>
</template>

<script>
import { getCarRuleList, deleteCarRule } from '@/api/car'
import { utils, writeFileXLSX } from 'xlsx'
import AddRule from './components/add-rule'
export default {
  name: 'Building',
  components: {
    AddRule
  },
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 4
      },
      total: 0,
      dialogVisible: false,
      currentId: null
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    chargeTypeMethod(type) {
      return type === 'duration' ? '时长收费' : type === 'turn' ? '按次收费' : type === 'partition' ? '分段收费' : '无'
    },
    async getRuleList() {
      const { data: { rows, total }} = await getCarRuleList(this.params)
      this.total = total
      this.ruleList = rows
    },
    async exportToExcelBtn() {
      const res = await getCarRuleList(this.params)
      // 表头英文字段key，为了对应res返回的数据
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // 表头中文字段value
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      const sheetData = res.data.rows.map(item => {
        const obj = {}
        tableHeaderKeys.forEach((key, index) => {
          if (key === 'chargeType') {
            obj[key] = this.chargeTypeMethod(item[key])
          } else {
            obj[key] = item[key]
          }
        })
        return obj
      })
      // 创建一个工作表
      const worksheet = utils.json_to_sheet(sheetData)
      // 创建一个工作簿
      const workbook = utils.book_new()
      // 将工作表添加到工作簿
      utils.book_append_sheet(workbook, worksheet, 'sheet1')
      // 添加表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      writeFileXLSX(workbook, '计费规则.xlsx')
    },
    async delBtn(id) {
      await this.$confirm('是否确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await deleteCarRule(id)
      this.$message.success('删除成功')
      this.getRuleList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getRuleList(this.params)
    },
    editBtn(id) {
      this.currentId = id
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
